package com.archstoday.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.adapter.SubscribeAdapter;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.SubscribeEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.archstoday.view.PullToRefreshLayout;
import com.base.http.common.BaseHttpClient;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;
import com.xw.repo.refresh.PullListView;

/**
 * 订阅
 */
public class SubscribeActivity extends AsScrollBaseActivity implements PullToRefreshLayout.OnRefreshListener {

    private TextView mTitleTv;
    private PullToRefreshLayout mPullLayout;
    private PullListView mPullList;

    private SubscribeAdapter mAdapter;
    private int mPage;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_list);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mPullLayout = (PullToRefreshLayout) findViewById(R.id.pull_layout);
        mPullList = (PullListView) findViewById(R.id.pull_list);
        mPullLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData(Bundle bundle) {
        mTitleTv.setText(R.string.user_take);

        if (StringUtil.isEmpty(MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""))){
            showToast(R.string.user_userid_null);
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            appFinish();
        } else {
            mAdapter = new SubscribeAdapter(mContext);
            mPullList.setAdapter(mAdapter);

            mPage = 1;
            upData();
        }
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);

        mPullList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext, TopsDetailsActivity.class);
                intent.putExtra("id", mAdapter.getItem(i).getArticleId());
                intent.putExtra("isNews", true);
                startActivity(intent);
            }
        });
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
        }
    }

    private void upData() {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("plat", "android");
        mParams.put("ifa", mApp.getDeviceID(mContext));
        mParams.put("page", mPage);
        mParams.put("pageSize", 10);

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.SUBSCRIBE_LIST),
                AsRes.SUBSCRIBE_LIST, mParams, SubscribeEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        mPullLayout.loadMoreFinish(true);
        mPullLayout.refreshFinish(true);
        if (client.getUrlIdentifier() == AsRes.SUBSCRIBE_LIST) {
            SubscribeEntity entity = (SubscribeEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                if (entity.getData().size() > 0) {
                    if (mPage == 1) {
                        mAdapter.setData(entity.getData());
                    } else {
                        mAdapter.addData(entity.getData());
                    }
                    mPage++;
                }
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
            } else {
                showToast(entity.getMsg());
            }
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        mPage = 1;
        upData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        upData();
    }
}